package com.employee.service.impl;

import com.employee.context.BaseContext;
import com.employee.mapper.ClockMapper;
import com.employee.pojo.dto.AgreeDTO;
import com.employee.pojo.dto.ReqDTO;
import com.employee.pojo.dto.ReqInfoDTO;
import com.employee.pojo.entry.Clock;
import com.employee.pojo.result.PageResult;
import com.employee.pojo.vo.ClockVO;
import com.employee.pojo.vo.ReqVO;
import com.employee.service.ClockService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ClockServiceImpl implements ClockService {

    @Autowired
    private ClockMapper clockMapper;

    @Override
    public Clock clockIn() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter);
        // 格式化当前时间
        String[] format = LocalDateTime.now().format(formatter).split(" ");

        Clock clock1 = clockMapper.selectByEmpId(BaseContext.getCurrentId(), format[0]);
        System.out.println(clock1);

        Clock clock = new Clock();
        if (clock1 == null) {
            // 定义时间格式
            clock.setEmpId(BaseContext.getCurrentId());
            clock.setDay(format[0]);
            clock.setClockInTime(format[1]);
            clock.setCreateTime(LocalDateTime.now());
            clock.setUpdateTime(LocalDateTime.now());
            clockMapper.insertIn(clock);
            return clock;
        }
        return null;
    }

    @Override
    public Clock clockOut() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter);
        // 格式化当前时间
        String[] format = LocalDateTime.now().format(formatter).split(" ");
        Clock clock = clockMapper.selectByEmpId(BaseContext.getCurrentId(), format[0]);
        if (clock != null) {
            clock.setClockOutTime(format[1]);
            clock.setUpdateTime(LocalDateTime.now());
            clockMapper.updateOut(clock);
            return clock;
        } else {
            Clock clockOut = new Clock();
            clockOut.setEmpId(BaseContext.getCurrentId());
            clockOut.setDay(format[0]);
            clockOut.setClockOutTime(format[1]);
            clockOut.setCreateTime(LocalDateTime.now());
            clockOut.setUpdateTime(LocalDateTime.now());
            clockMapper.insertIn(clockOut);
            return clockOut;
        }
    }

    @Override
    public ClockVO getClock() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter);
        String[] format = LocalDateTime.now().format(formatter).split(" ");
        return clockMapper.selectClockInfo(BaseContext.getCurrentId(), format[0]);
    }

    @Override
    public PageResult<ReqVO> notClock(ReqInfoDTO reqInfoDTO) {
        reqInfoDTO.setEmpId(BaseContext.getCurrentId());
        PageHelper.startPage(reqInfoDTO.getPageNum(), reqInfoDTO.getPageSize());
        Page<ReqVO> req = clockMapper.noClock(reqInfoDTO);
        System.out.println(req);
        int total = (int) req.getTotal();
        List<ReqVO> reqVOList = req.getResult();
        return new PageResult<>(total, reqVOList);
    }

    @Override
    public void AddClock(ReqDTO reqDTO) {
        clockMapper.updateClock(reqDTO);
    }

    @Override
    public void AgreeReq(AgreeDTO agreeDTO) {
        clockMapper.AgreeReq(agreeDTO);
    }
}
